<?php

/**
 * login actions.
 *
 * @package    SCBE
 * @subpackage login
 * @author     SCBE
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
  public function executeAcesso(sfWebRequest $request)
  {
    $this->getUser()->logout();
//    $this->getUser()->setAttribute('logado', false);
    $this->getUser()->getAttributeHolder();
  }
  
  public function executeAutenticar(sfWebRequest $request)
  {
      if (trim($request->getParameter('login'))=='' ||  trim($request->getParameter('senha'))=='') {
          $this->setTemplate('acesso');
      }
      else{
          if (Doctrine::getTable('cadusuarios')->recuperarLogin($request->getParameter('login'))) {
              $objlogin = Doctrine::getTable('cadusuarios')->recuperarSenha($request->getParameter('login'),md5($request->getParameter('senha')));
              if ($objlogin) {
                  $this->getUser()->autentica();
                  $usuario = Doctrine::getTable('cadusuarios')->recuperarUsuario($request->getParameter('login'));
                  $this->getUser()->setAttribute('usuario', $usuario[0]['Nome']);
                  $this->getUser()->setAttribute('idusuarios', $usuario[0]['IdUsuarios']);
                  $this->redirect('analise/recebimento');
              }
              else{
                  $this->getUser()->setFlash('aviso', 'Usuário ou senha incorretos.');
                  $this->setTemplate('acesso');
              }
          }
          else{
              $this->getUser()->setFlash('aviso', 'Usuário ou senha incorretos.');
              $this->setTemplate('acesso');
          }
      }    
  }
  public function executeIndex(sfWebRequest $request)
  {
    $this->cadusuarioss = Doctrine_Core::getTable('cadusuarios')
      ->createQuery('a')
//      ->addWhere('a.cpf=','\''.$request->getParameter('cpf').'\'')      
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->cadusuarios = Doctrine_Core::getTable('cadusuarios')->find(array($request->getParameter('idusuarios')));
    $this->forward404Unless($this->cadusuarios);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new cadusuariosForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new cadusuariosForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($cadusuarios = Doctrine_Core::getTable('cadusuarios')->find(array($request->getParameter('idusuarios'))), sprintf('Object cadusuarios does not exist (%s).', $request->getParameter('idusuarios')));
    $this->form = new cadusuariosForm($cadusuarios);
  }
  
  public function executeSobre()
  {
    $this->setTemplate('sobre');
//    $this->redirect('login/sobre');
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($cadusuarios = Doctrine_Core::getTable('cadusuarios')->find(array($request->getParameter('idusuarios'))), sprintf('Object cadusuarios does not exist (%s).', $request->getParameter('idusuarios')));
    $this->form = new cadusuariosForm($cadusuarios);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($cadusuarios = Doctrine_Core::getTable('cadusuarios')->find(array($request->getParameter('idusuarios'))), sprintf('Object cadusuarios does not exist (%s).', $request->getParameter('idusuarios')));
    $cadusuarios->delete();

    $this->redirect('login/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  { 
    $values = $request->getParameter($form->getName());
    $values['senha'] = md5($values['senha']);
    
    $form->bind($values, $request->getFiles($form->getName()));

    if ($form->isValid())
    { 
      $cadusuarios = $form->save();
      
      $this->redirect('login/edit?idusuarios='.$cadusuarios->getIdusuarios());
    }
  }
//  {
//    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
//    if ($form->isValid())
//    {
//      $cadusuarios = $form->save();
//
//      $this->redirect('login/edit?idusuarios='.$cadusuarios->getIdusuarios());
//    }
//  }
  
  public function executeAbrirDocumento(sfWebRequest $request)
  {
    $documento = $request['documento'];

    $fileName = "../web/uploads/manual/"."{$documento}.pdf";

    if (file_exists($fileName))
    {
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setStatusCode(200);
        $this->getResponse()->setContentType('application/pdf');
        $this->getResponse()->setHttpHeader('Pragma', 'public'); //optional cache header
        $this->getResponse()->setHttpHeader('Expires', 0); //optional cache header
        $this->getResponse()->setHttpHeader('Content-Disposition', "attachment; filename={$documento}.pdf");
        $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
        $this->getResponse()->setHttpHeader('Content-Length', filesize($fileName));

        return $this->renderText(file_get_contents($fileName));
    } else {
        $this->redirect('login/AbrirDocumento');
    }
  } 
}
